package com.ctshk.sapp.order.scenic.controller;


import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.order.scenic.dto.AirshowOrderDTO;
import com.ctshk.rpc.order.scenic.req.AirshowOrderCancelReq;
import com.ctshk.rpc.order.scenic.req.AirshowOrderCreateReq;
import com.ctshk.rpc.order.scenic.req.AirshowOrderListReq;
import com.ctshk.rpc.order.scenic.service.IAirshowOrderService;
import com.ctshk.rpc.scenic.dto.AirshowProductDTO;
import com.ctshk.rpc.scenic.dto.AirshowSceneDTO;
import com.ctshk.rpc.scenic.dto.AirshowTicketRespDTO;
import com.ctshk.rpc.scenic.req.AirshowApiSettingReq;
import com.ctshk.rpc.scenic.req.AirshowProductListReq;
import com.ctshk.rpc.scenic.service.IAirshowApiSettingService;
import com.ctshk.rpc.scenic.service.IAirshowDistributorSettingService;
import com.ctshk.rpc.scenic.service.IAirshowProductReserveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-06-10
 */
@Api(value="航展分销商API服务", tags="航展分销商API服务")
@RestController
@RequestMapping("/airshow-api")
public class AirshowApiController extends BaseController {

    @DubboReference
    private IAirshowDistributorSettingService iAirshowDistributorSettingService;

    @DubboReference
    private IAirshowProductReserveService iAirshowProductReserveService;

    @DubboReference
    private IAirshowOrderService iAirshowOrderService;

    @ApiOperation(value="API-获取航展门票产品", notes="API-获取航展门票产品")
    @GetMapping("/get-ticket-products")
    public Result<List<AirshowTicketRespDTO>> getTicketList(){
        String appkey = getRequest().getHeader("appkey");
        return iAirshowProductReserveService.getTicketList(iAirshowDistributorSettingService.getAPIToken(appkey));
    }

    @ApiOperation(value="API-获取航展门票场次列表", notes="API-获取航展门票场次列表")
    @GetMapping("/get-ticket-scene")
    public Result<List<AirshowSceneDTO>> getTicketScene(){
        String appkey = getRequest().getHeader("appkey");
        return iAirshowProductReserveService.getTicketScene(iAirshowDistributorSettingService.getAPIToken(appkey));
    }

    @ApiOperation(value="API-通过场次ID查询门票列表", notes="API-通过场次ID查询门票列表")
    @GetMapping("/get-ticket-by-scene-id")
    public Result<List<AirshowTicketRespDTO>> getTicketBySceneId(Long sceneId){
        String appkey = getRequest().getHeader("appkey");
        return iAirshowProductReserveService.getTicketBySceneId(sceneId,iAirshowDistributorSettingService.getAPIToken(appkey));
    }

    @ApiOperation(value="API-查询套票产品列表", notes="API-查询套票产品列表")
    @PostMapping("/get-package-products")
    public PageResponse<AirshowProductDTO> getProductListByParams(@RequestBody AirshowProductListReq airshowProductReq){
        String appkey = getRequest().getHeader("appkey");
        return iAirshowProductReserveService.getProductListByParams(airshowProductReq,iAirshowDistributorSettingService.getAPIToken(appkey));
    }

    @ApiOperation(value = "API-航展门票下单", notes = "API-航展门票下单")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.ADD, operTitle = "珠海航展-分销商api下单", operDesc = "珠海航展-分销商api下单")
    @PostMapping(value = "/create-ticket-product-order")
    public Result agentCreateAPIOrder(@RequestBody AirshowOrderCreateReq airshowOrderCreateReq){
        String appkey = getRequest().getHeader("appkey");
        return iAirshowOrderService.agentCreateAPIOrder(airshowOrderCreateReq,iAirshowDistributorSettingService.getAPIToken(appkey),0);
    }

    @ApiOperation(value = "API-航展门票下单(不支付)", notes = "API-航展门票下单(不支付)")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.ADD, operTitle = "珠海航展-航展门票下单(不支付)", operDesc = "珠海航展-航展门票下单(不支付)")
    @PostMapping(value = "/create-ticket-product-order-not-pay")
    public Result agentCreateAPIOrderNotPay(@RequestBody AirshowOrderCreateReq airshowOrderCreateReq){
        String appkey = getRequest().getHeader("appkey");
        return iAirshowOrderService.agentCreateAPIOrderNotPay(airshowOrderCreateReq,iAirshowDistributorSettingService.getAPIToken(appkey),0);
    }

    @ApiOperation(value = "API-航展套票下单", notes = "API-航展套票下单")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.ADD, operTitle = "珠海航展-分销商套票下单", operDesc = "珠海航展-分销商套票下单")
    @PostMapping(value = "/create-package-product-order")
    public Result agentCreatePackageOrder(@RequestBody AirshowOrderCreateReq airshowOrderCreateReq){
        String appkey = getRequest().getHeader("appkey");
        return iAirshowOrderService.agentCreatePackageOrder(airshowOrderCreateReq,iAirshowDistributorSettingService.getAPIToken(appkey),0);
    }

    @ApiOperation(value = "API-航展套票下单(不支付)", notes = "API-航展套票下单(不支付)")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.ADD, operTitle = "珠海航展-航展套票下单(不支付)", operDesc = "珠海航展-航展套票下单(不支付)")
    @PostMapping(value = "/create-package-product-order-not-pay")
    public Result agentCreatePackageOrderNotPay(@RequestBody AirshowOrderCreateReq airshowOrderCreateReq){
        String appkey = getRequest().getHeader("appkey");
        return iAirshowOrderService.agentCreatePackageOrderNotPay(airshowOrderCreateReq,iAirshowDistributorSettingService.getAPIToken(appkey),0);
    }

    @ApiOperation(value = "API-分销商查询订单列表", notes = "API-分销商查询订单列表")
    @PostMapping(value = "/get-orders")
    public Result getAgentOrderListByParams(@RequestBody AirshowOrderListReq airshowOrderListReq){
        String appkey = getRequest().getHeader("appkey");
        return iAirshowOrderService.getAgentOrderListByParams(airshowOrderListReq,iAirshowDistributorSettingService.getAPIToken(appkey));
    }

    @ApiOperation(value = "API-分销商订单发起退票申请", notes = "API-分销商订单发起退票申请")
    @OperationLogAnnotation(operModul = OperationLogModel.TICKET, operType = OperationLogType.UPDATE, operTitle = "珠海航展-分销商订单发起退票", operDesc = "珠海航展-分销商订单发起退票")
    @PostMapping(value = "/cancel-order")
    public Result agentCancelOrderById(@RequestBody AirshowOrderCancelReq airshowOrderCancelReq){
        String appkey = getRequest().getHeader("appkey");
        return iAirshowOrderService.agentCancelOrderById(airshowOrderCancelReq,iAirshowDistributorSettingService.getAPIToken(appkey));
    }

    @ApiOperation(value = "API-通过订单ID查询订单", notes = "API-通过订单ID查询订单")
    @GetMapping(value = "/get-order")
    public Result<AirshowOrderDTO> getOrderById(Long id){
        return iAirshowOrderService.getOrderById(id);
    }

    @ApiOperation(value = "API-支付订单", notes = "API-支付订单")
    @GetMapping(value = "/pay-order")
    public Result payOrder(Long id){
        return iAirshowOrderService.payOrder(id);
    }

    @ApiOperation(value = "API-查询分销商余额", notes = "API-查询分销商余额")
    @GetMapping(value = "/get-balance")
    public Result getDistributorBalanceById(){
        String appkey = getRequest().getHeader("appkey");
        TokenUser apiToken = iAirshowDistributorSettingService.getAPIToken(appkey);
        if(apiToken == null){
            throw new BusinessException(SystemError.USER_1025);
        }
        return iAirshowDistributorSettingService.getDistributorBalanceById(apiToken.getId());
    }

}
